package cn.lyh.myiot.consumer;

import cn.lyh.myiot.pojo.WenshiduTable;
import cn.lyh.myiot.pojo.mapper.WenshiduTableMapper;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * topic 消息消费者类
 * @author lyh
 * @version 1.0
 * @date 2020/8/13
 */
@Component
public class Consumer {

    final
    WenshiduTableMapper wenshiduTableMapper;

    public Consumer(WenshiduTableMapper wenshiduTableMapper) {
        this.wenshiduTableMapper = wenshiduTableMapper;
    }

    /**
     * 订阅一个topic主题，配置一个jms监听器
     * @param msg 监听器接受到的消息
     */
    @JmsListener(destination = "mqtt", containerFactory = "topicListener")
    public void listenTopic(char[] msg) {
//        获取到数据转为java对象
        String s = String.valueOf(msg);
        WenshiduTable wenshiduTable = JSONObject.parseObject(s).toJavaObject(WenshiduTable.class);
        System.out.println(wenshiduTable);
//        存入数据库
        wenshiduTableMapper.insert(wenshiduTable);
    }

}
